package leetcode.f1t100;

/**
 * 顺时针旋转矩阵
 * https://leetcode.com/problems/rotate-image/
 *
 * @Author lichongxiao
 * @Date 2022/10/24 17:33
 */
public class Q48_RotateImage {

    public void rotate(int[][] matrix) {
        int length = matrix.length, midUp = (length + 1) / 2,midDown = (length) / 2, maxIndex = length - 1;
        for (int i = 0; i < midUp; i++) {
            for (int j = 0; j < midDown; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[maxIndex -j][i];
                matrix[maxIndex -j][i] = matrix[maxIndex - i][maxIndex - j];
                matrix[maxIndex - i][maxIndex - j] = matrix[j][maxIndex - i];
                matrix[j][maxIndex - i] = temp;
            }
        }
    }

    public static void main(String[] args) {

        Q48_RotateImage bean = new Q48_RotateImage();
        int[][] martix = {{1, 2}, {4, 5}};
        for (int[] array : martix) {
            for (int num : array) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
        bean.rotate(martix);
        for (int[] array : martix) {
            for (int num : array) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }


}
